package cn.stylefeng.guns.modular.project.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.project.entity.Project;
import cn.stylefeng.guns.modular.project.mapper.ProjectMapper;
import cn.stylefeng.guns.modular.project.mapper.ProjectMemberWorkMapper;
import cn.stylefeng.guns.modular.project.model.params.ProjectParam;
import cn.stylefeng.guns.modular.project.model.result.ProjectResult;
import cn.stylefeng.guns.modular.project.service.ProjectMemberService;
import cn.stylefeng.guns.modular.project.service.ProjectMemberWorkService;
import cn.stylefeng.guns.modular.project.service.ProjectService;
import cn.stylefeng.guns.modular.reimbursement.service.ReimbursementService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 项目信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-10-15
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Autowired
    private ProjectMemberWorkService projectMemberWorkService;
    @Autowired
    private ProjectMemberService projectMemberService;
    @Autowired
    private ReimbursementService reimbursementService;

    @Override
    public void add(ProjectParam param) {
        Project entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ProjectParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ProjectParam param) {
        Project oldEntity = getOldEntity(param);
        Project newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ProjectResult findBySpec(ProjectParam param) {
        return null;
    }

    @Override
    public List<ProjectResult> findListBySpec(ProjectParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ProjectParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<ProjectResult> records = page.getRecords();

        for (ProjectResult projectResult : records) {
            // 合计工时
            int sumWorkHour = projectMemberWorkService.sumWorkHour(projectResult.getId());
            projectResult.setWorkHour(sumWorkHour);
            // 项目人数
            int countMember = projectMemberService.countMember(projectResult.getId());
            projectResult.setCountMember(countMember);
            // 发生费用(合计)
            projectResult.setMoney(reimbursementService.findMoneyByProject(projectResult.getId(),""));
            // 发生费用(饭费)
            projectResult.setMoneyMeal(reimbursementService.findMoneyByProject(projectResult.getId(),"02"));
            // 发生费用(交通费)
            projectResult.setMoneyTrafficExpense(reimbursementService.findMoneyByProject(projectResult.getId(),"01"));
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ProjectResult> findAllProject() {
        return this.baseMapper.allProject();
    }

    private Serializable getKey(ProjectParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Project getOldEntity(ProjectParam param) {
        return this.getById(getKey(param));
    }

    private Project getEntity(ProjectParam param) {
        Project entity = new Project();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
